/*
 *
 * Copyright(c) 2018-2019 chj <1019487871@qq.com>
 * QQ: 1019487871
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
#include "los.h"
#include "lvgl.h"
#include "demo.h"
#include "fundation/malloc/los_malloc.h"

#define LOS_MAIN_THREAD_STACK_SIZE      (1200U)
#define LOS_LED_THREAD_STACK_SIZE       (400U)

static uint32_t led_mask[] = {1UL << 9, 1UL << 10};

void
losbsp_systick_handler(void)
{
    los_tick_isr(1);
    lv_tick_inc(1);
    los_int_switch_thread();
}


void
los_led_on(uint32_t num)
{
    GPIOF->BSRR = led_mask[num];
}

void
los_led_off(uint32_t num)
{
    GPIOF->BSRR = led_mask[num]<<16;
}

static void
led_thread(uint32_t *param)
{
    param = *(&param);
    while(1)
    {
        los_led_on(0);
        los_delay_ms(1000);
        los_led_off(0);
        los_delay_ms(1000);
    }
}

static void
los_led_init(void)
{
    struct los_thread_t *__led_thread = NULL;
    RCC->AHB1ENR|=1<<5;//使能PORTF时钟
    GPIOF->MODER    &= ~((3UL << 2*9) | (3UL << 2*10));
    GPIOF->MODER    |=  ((1UL << 2*9) | (1UL << 2*10));
    GPIOF->OTYPER   &= ~((1UL << 2*9) | (1UL << 2*10));
    GPIOF->OSPEEDR  &= ~((3UL << 2*9) | (3UL << 2*10));
    GPIOF->OSPEEDR  |=  ((2UL << 2*9) | (2UL << 2*10));
    GPIOF->PUPDR    &= ~((3UL << 2*9) | (3UL << 2*10));
    GPIOF->PUPDR    |=  ((1UL << 2*9) | (1UL << 2*10));
    __led_thread = los_resiger_thread(led_thread,NULL,LOS_LED_THREAD_STACK_SIZE,125);
    if(__led_thread!=NULL)
        los_start_thread(__led_thread);
}

static void
main_thread(uint32_t *param)
{
    param = *(&param);
    los_led_init();
    ui_init();
    while(1)
    {
        los_led_on(1);
        los_delay_ms(1000);
        los_led_off(1);
        los_delay_ms(1000);
    }
}

int
main(void)
{
    struct los_thread_t *__main_thread = NULL;
    losbsp_tick_init();
    __main_thread = los_resiger_thread(main_thread,NULL,LOS_MAIN_THREAD_STACK_SIZE,126);
    if(__main_thread!=NULL)
        los_start_thread(__main_thread);
    los_start();
    return 0;
}
